package com.hfjyz.judge.judgecall.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.judge.judgecall.bean.JudgeCallBean;
import com.hfjyz.judge.judgecall.bean.JudgeCallExcelBean;
import com.hfjyz.judge.judgecall.bean.JudgeCallSearchBean;

public class JudgeCallExcelAction extends JudgeCallAction {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -7168323322187048129L;
    private static final String XLS_EXPERT_TEMPLATE = "judgeCallTemplate.xls";

    private String downloadFileName = null;
    private InputStream excelStream = null;

    public String getDownloadFileName() {
        return downloadFileName;
    }

    public void setDownloadFileName(String downloadFileName) {
        this.downloadFileName = downloadFileName;
    }

    public InputStream getExcelStream() {
        return excelStream;
    }

    public void setExcelStream(InputStream excelStream) {
        this.excelStream = excelStream;
    }

    public String exportExcel() {
        ByteArrayOutputStream baos = null;
        try {
            String rootPath = ServletActionContext.getServletContext().getRealPath("/");
            String templatePath = rootPath + "WEB-INF\\conf\\judge\\" + XLS_EXPERT_TEMPLATE;

            HttpSession session = ServletActionContext.getRequest().getSession();
            Object o = session.getAttribute(BaseConstant.SESSION_CONDITION_KEY);
            JudgeCallSearchBean searchBean = null;
            if (o instanceof JudgeCallSearchBean) {
                searchBean = (JudgeCallSearchBean) o;
            }
            if (searchBean == null) {
                searchBean = judgeCallSearch;
            }
            List<JudgeCallBean> judgeCallList = getService().findJudgeCall(getSearchCondition(), searchBean);
            List<JudgeCallExcelBean> excelList = getExcelBeanList(judgeCallList);

            Workbook wb = getService().exportJudgeCall(templatePath, excelList);
            baos = new ByteArrayOutputStream();
            wb.write(baos);
            baos.flush();
            byte[] excelBytes = baos.toByteArray();
            excelStream = new ByteArrayInputStream(excelBytes, 0, excelBytes.length);

            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            downloadFileName = "评委调用详细_" + sf.format(new Date()) + ".xls";
            downloadFileName = new String(downloadFileName.getBytes(), "ISO8859-1");
        } catch (Exception e) {
            map.put("errorMsg", e.getMessage());
        }
        return SUCCESS;
    }
    
    private List<JudgeCallExcelBean> getExcelBeanList(List<JudgeCallBean> judgeCallList) {
        List<JudgeCallExcelBean> beanList = new ArrayList<JudgeCallExcelBean>();
        if (judgeCallList == null || judgeCallList.isEmpty()) {
            return beanList;
        }
        
        for (JudgeCallBean judgeCall : judgeCallList) {
            beanList.add(new JudgeCallExcelBean(judgeCall));
        }
        return beanList;
    }
}
